#include <ros/ros.h>
#include "loop_addition_action/AddIntsAction.h"
#include <actionlib/client/simple_action_client.h>

//将 Actionlib::SimpleActionClient<loop_addition_action::AddIntsAction>定义别名Client
typedef actionlib::SimpleActionClient<loop_addition_action::AddIntsAction> Client;


//当Action执行完成后调用的回调函数
void done_cb(const actionlib::SimpleClientGoalState &state, const loop_addition_action::AddIntsResultConstPtr &result)
{
//判断Action执行状态
    if (state.state_ == state.SUCCEEDED)
    {
        ROS_INFO("最终结果:%d",result->result);
    } else {
        ROS_INFO("任务失败！");
    }

}
//Action激活后会调用的回调函数
void active_cb()
{
    ROS_INFO("服务已经被激活....");
}
//收到feedback后调用的回调函数
void  feedback_cb(const loop_addition_action::AddIntsFeedbackConstPtr &feedback)
{
    ROS_INFO("当前进度:%d %%",(int)(feedback->progress_bar *100));
    
}

int main(int argc, char *argv[])
{
    setlocale(LC_ALL,"");
    // 初始化ROS节点;
    ros::init(argc,argv,"AddInts_client");
    // 创建NodeHandle;
    ros::NodeHandle nh;
    // 创建Action客户端对象;
    // SimpleActionClient(ros::NodeHandle & n, const std::string & name, bool spin_thread = true)
    Client client(nh,"addInts",true);
    //等待服务启动
    client.waitForServer();
    // 发送目标，处理反馈以及最终结果;
    /*  sendGoal函数参数
    void sendGoal(const loop_addition_action::AddIntsGoal &goal, 
            	boost::function<void (const Actionlib::SimpleClientGoalState &state, const loop_addition_action::AddIntsResultConstPtr &result)> done_cb, 
            boost::function<void ()> active_cb, 
            boost::function<void (const loop_addition_action::AddIntsFeedbackConstPtr &feedback)> feedback_cb)
    */
    loop_addition_action::AddIntsGoal goal;
    goal.num = 100;
    client.sendGoal(goal, &done_cb, &active_cb, &feedback_cb);
    ros::spin();
    return 0;
}
